# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import AbstractUser

# from company.models import MaintainCompanyInfo, PropertyCompanyInfo
from utils.models import BaseModel


class AuthUser(AbstractUser):
    username = models.CharField(max_length=50, verbose_name=u'账户名称', blank=True, null=True, unique=True, db_index=True)
    real_name = models.CharField(max_length=50, verbose_name=u'员工真实姓名', blank=True, null=True, db_index=True)
    phone = models.CharField(max_length=11, verbose_name=u'手机号', blank=True, null=True, db_index=True)
    image = models.CharField(max_length=256, verbose_name=u"用户头像", blank=True, null=True)
    sign_image = models.CharField(max_length=256, verbose_name=u"签名照片", blank=True, null=True)
    gender = models.SmallIntegerField(verbose_name=u"性别", null=True, blank=True)
    session = models.CharField(max_length=50, verbose_name=u"随机字符串", default=u"", db_index=True)
    R_0 = 0
    R_1 = 10
    R_2 = 20
    R_3 = 30
    R_4 = 40
    ROLE_CHOICES = (
        (R_0, u"超级管理员"),
        (R_1, u"维保人员"),
        (R_2, u"维保管理人员"),
        (R_3, u"物业管理人员"),
        (R_4, u"监管单位人员"),
    )
    role = models.IntegerField(default=R_1, choices=ROLE_CHOICES, verbose_name=u"角色", db_index=True)
    admin_role = models.ForeignKey('AdminRole', verbose_name=u'角色权限', db_index=True, null=True)
    desc = models.CharField(max_length=200, verbose_name=u'备注', default='', null=True, blank=True)
    service_id = models.IntegerField(verbose_name=u"对应维保公司id", default=0, db_index=True)
    update_by = models.IntegerField(verbose_name=u"更新人员", default=0)
    PT1 = 10
    PT2 = 20
    PT = (
        (PT1, u"角色权限"),
        (PT2, u"自选权限"),
    )
    permission_type = models.SmallIntegerField(choices=PT, verbose_name=u"权限类型", db_index=True, default=PT1)
    # 当用户选择 梯联网平台时 organization为空，permit_all为True，此时可以选择所有电梯
    # organization = models.ForeignKey('Organization', verbose_name=u"组织结构", null=True, db_index=True)
    permit_all = models.BooleanField(default=False, verbose_name=u"是否所有的")

    class Meta:
        verbose_name = "系统登录账户"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.username

    def department(self):
        admin_role = self.admin_role
        oarr = OrganizationAdminRoRelation.objects.filter_active(admin_role=admin_role).first()
        if oarr and oarr.organization:
            return oarr.organization.name
        else:
            return None

    def org(self):
        oid = None
        if self.admin_role:
            oarr = OrganizationAdminRoRelation.objects.filter_active(admin_role=self.admin_role).first()
            if oarr and oarr.organization:
                oid = oarr.organization_id
        return oid

    def permissions(self):
        if self.username == u'admin':
            permissions = ["0"]
        elif self.admin_role:
            permissions = self.admin_role.permissions()
        else:
            permissions = []
        return permissions

    def admin_role_name(self):
        return self.admin_role.role_name if self.admin_role else None

    def elevators(self):
        l = []
        if self.role == 0:
            return l
        if self.permission_type == 20:
            l = AuthUserElevatorRelation.objects.filter_active(auth_user=self).values_list("register_code", flat=True).distinct()
        elif self.permission_type == 10 and self.admin_role:
            if self.admin_role.management_type == 20:
                l = self.admin_role.register_codes()
            elif self.admin_role.management_type == 10:
                oarr = OrganizationAdminRoRelation.objects.filter_active(admin_role=self.admin_role).first()
                if oarr and oarr.organization:
                    l = OrganizationElevatorRelation.objects.filter_active(organization=oarr.organization, service_id=self.service_id).values("register_code", "internal_no", "maintain_company_name", "property_company_name")
                    l = [item['register_code'] for item in l]
            else:
                l = []
        return l
        # return []

    def org_permit(self):
        oids = []
        if self.role == 0:
            oids = ["all"]
        elif self.role == 10:
            pass
        elif self.role in (20, 30, 40) and self.company_person.is_admin:
            if self.admin_role:
                if self.role == 20:
                    oids.append(-1)
                oarr = OrganizationAdminRoRelation.objects.filter_active(admin_role=self.admin_role).first()
                if oarr and (oarr.company == self.company_person.company or oarr.is_top) and self.role == 20:
                    # ids = Organization.objects.filter_active(company=self.company_person.company).values_list("id", flat=True).distinct()
                    service_id = self.company_person.company.service_id
                    ids = CompanyInfo.objects.filter_active(service_id=service_id, company_type=300).values_list("id", flat=True).distinct()
                    oids.extend(ids)

        else:
            if self.admin_role:
                oarr = OrganizationAdminRoRelation.objects.filter_active(admin_role=self.admin_role).first()
                if oarr and oarr.organization:
                    oids = oarr.organization.children()
        return oids

    def update_by_user(self):
        uid = self.update_by
        if uid == 0:
            return u"admin"
        if uid is None:
            return ""
        user = AuthUser.objects.filter(id=uid).first()
        return user.username if user else ""

    def is_admin(self):
        if self.role == 0 and self.username == u"admin":
            return 1
        if self.role in (20, 30, 40):
            cp = CompanyPerson.objects.filter_active(auth_user=self).first()
            if cp and cp.is_admin:
                return 1
        return 0

    def registers(self):
        import json
        rec = []
        send = []
        try:
            person = self.person
        except:
            person = None
        if person:
            registry = Registry.objects.filter(type_for_reg='Person', reg_obj_id=person.id)
            for i in registry:
                if i.reg_info:
                    send.append(json.loads(i.reg_info))
                if i.ret_info:
                    rec.append(json.loads(i.ret_info))
        return {"rec": rec, "send": send}


class AuthUserElevatorRelation(BaseModel):

    auth_user = models.ForeignKey(AuthUser, verbose_name=u"账户", null=True, blank=True, db_index=True)
    register_code = models.CharField(max_length=20, verbose_name=u"电梯注册代码", default=True)
    internal_no = models.CharField(max_length=50, verbose_name=u"内部编号", null=True, blank=True, db_index=True)
    maintain_company_name = models.CharField(max_length=256, verbose_name=u"维保单位名称", null=True, blank=True)
    property_company_name = models.CharField(max_length=256, verbose_name=u"使用单位名称", null=True, blank=True)
    is_business = models.BooleanField(default=True, verbose_name=u"是否是维保商业电梯")

    def to_json(self):
        return {
            "auth_user": self.auth_user.id,
            "register_code": self.register_code,
            "internal_no": self.internal_no,
            "maintain_company_name": self.maintain_company_name,
            "property_company_name": self.property_company_name
        }


class CompanyInfo(BaseModel):
    COM_MAINTAIN = 100
    COM_USER = 200
    COM_PROPERTY = 300
    COM_SUPERVISION = 400
    COM_TYPE_CHOICES = (
        (COM_MAINTAIN, u"维保公司"),
        (COM_USER, u"使用公司"),
        (COM_PROPERTY, u"物业公司"),
        (COM_SUPERVISION, u"监管单位")
    )
    company_type = models.PositiveIntegerField(u"公司类型", default=COM_MAINTAIN, choices=COM_TYPE_CHOICES, db_index=True)
    link_account = models.CharField(max_length=256, verbose_name=u"对接账号", blank=True, null=True)
    link_token = models.CharField(max_length=256, verbose_name=u"对接令牌", blank=True, null=True)
    # auth_user = models.OneToOneField(AuthUser, verbose_name=u"账户", null=True, blank=True, on_delete=models.SET_NULL,
    #                                  related_name="company_info")
    name = models.CharField(max_length=256, verbose_name=u"企业名称", blank=True, null=True)
    company_telephone = models.CharField(max_length=20, verbose_name=u"单位电话", blank=True, null=True, db_index=True)
    telephone = models.CharField(max_length=20, verbose_name=u"单位电话", blank=True, null=True, db_index=True)
    register_address = models.CharField(max_length=256, verbose_name=u"详细地址", blank=True, null=True)
    address = models.CharField(max_length=256, verbose_name=u"详细地址", blank=True, null=True)
    unified_code = models.CharField(max_length=256, verbose_name=u"社会统一信用代码", blank=True, null=True)
    person_in_charge = models.CharField(max_length=20, verbose_name=u"主要负责人", blank=True, null=True, db_index=True)
    person_in_charge_phone = models.CharField(max_length=20, verbose_name=u"负责人电话", blank=True, null=True, db_index=True)
    legal_person = models.CharField(max_length=20, verbose_name=u"法人", blank=True, null=True, db_index=True)
    legal_phone = models.CharField(max_length=20, verbose_name=u"法人手机", blank=True, null=True, db_index=True)
    business_lic = models.CharField(max_length=256, verbose_name=u"营业执照网络地址", blank=True, null=True)
    lic_pic = models.CharField(max_length=256, verbose_name=u"特种设备安装改造维修许可证网络地址", blank=True, null=True)
    register_province = models.CharField(max_length=128, verbose_name=u"注册省份代码", null=True, blank=True, db_index=True)
    register_city = models.CharField(max_length=128, verbose_name=u"注册城市代码", null=True, blank=True, db_index=True)
    register_county = models.CharField(max_length=128, verbose_name=u"注册区/县代码", null=True, blank=True, db_index=True)
    register_street = models.CharField(max_length=128, verbose_name=u"注册地街道代码", null=True, blank=True, db_index=True)
    REGISTRY_1 = 10
    REGISTRY_2 = 20
    REGISTRY_3 = 30
    REGISTRY = (
        (10, u'未备案'),
        (20, u'备案成功'),
        (30, u'备案失败'),
    )
    registry_status = models.IntegerField(choices=REGISTRY, default=REGISTRY_1, verbose_name=u'备案状态', db_index=True)
    registry_time = models.DateTimeField(verbose_name=u'备案完成时间', null=True, blank=True)
    title = models.CharField(verbose_name="web标题", default="", null=True, blank=True, max_length=128)
    create_by = models.ForeignKey(AuthUser, verbose_name=u"创建者", blank=True, null=True)
    hour_price = models.DecimalField(max_digits=16, decimal_places=2, default=0, verbose_name=u"工时价")

    class Meta:
        verbose_name = u"单位信息表"
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.name

    def create_user(self):
        return self.create_by.username if self.create_by else ""


class CompanyAutoSubmit(BaseModel):
    company = models.ForeignKey(CompanyInfo, verbose_name=u"公司", null=True, blank=True, related_name='company_auto_submit', db_index=True)
    auto_submit = models.BooleanField(verbose_name=u"是否自动提交数据到市局", default=True, db_index=True)


# 维保公司网点信息表
class CompanyWebsiteInfo(BaseModel):
    company = models.ForeignKey(CompanyInfo, verbose_name=u"公司", null=True, blank=True, related_name='company_webinfo', db_index=True)
    office_address = models.CharField(max_length=100, verbose_name=u"维保网点地址", null=True, blank=True)
    province_code = models.CharField(max_length=20, verbose_name=u"维保网点省份区域代码", default= 32, null=True,
                                     blank=True, db_index=True)
    city_code = models.CharField(max_length=20, verbose_name=u"维保网点城市区域代码", null=True, blank=True, db_index=True)
    county_code = models.CharField(max_length=20, verbose_name=u"维保网点区县、乡镇区域代码", null=True, blank=True, db_index=True)
    street_code = models.CharField(max_length=20, verbose_name=u"维保网点街道区域代码", null=True, blank=True, db_index=True)
    emergency_phone = models.CharField(max_length=20, verbose_name=u"24小时应急值守电话", null=True, blank=True, db_index=True)
    office_main_person = models.CharField(max_length=20, verbose_name=u"网点负责人", null=True, blank=True, db_index=True)
    office_main_person_phone = models.CharField(max_length=11, verbose_name=u"网点负责人手机", null=True, blank=True, db_index=True)

    class Meta:
        verbose_name = u"维保公司网点信息表"
        verbose_name_plural = verbose_name

    # def __unicode__(self):
    #     return self.company.name


class CompanyPerson(BaseModel):
    # company = models.IntegerField(verbose_name=u"公司", null=True, blank=True)
    company = models.ForeignKey(CompanyInfo, verbose_name=u"公司", null=True, blank=True,  on_delete=models.SET_NULL,
                                related_name='company_person', db_index=True)
    auth_user = models.OneToOneField(AuthUser, verbose_name=u"账户", null=True, blank=True,  on_delete=models.SET_NULL,
                                     related_name='company_person', db_index=True)
    P_T_1 = 10
    P_T_2 = 20
    P_T = (
        (10, u'维保公司人员'),
        (20, u'使用公司人员')
    )
    person_type = models.IntegerField(choices=P_T, default=P_T_1, verbose_name=u'人员类型', db_index=True)
    is_admin = models.BooleanField(default=False, verbose_name=u"是否是企业管理员", db_index=True)


class Person(BaseModel):

    # link_account = models.CharField(max_length=256, verbose_name=u"对接账号", blank=True, null=True)
    # link_token = models.CharField(max_length=256, verbose_name=u"对接令牌", blank=True, null=True)
    company = models.ForeignKey(CompanyInfo, verbose_name=u"公司", null=True, blank=True,  on_delete=models.SET_NULL, db_index=True)
    auth_user = models.OneToOneField(AuthUser, verbose_name=u"账户", null=True, blank=True,  on_delete=models.SET_NULL, db_index=True)
    STATUS_ON = 10
    STATUS_OFF = 20
    STATUS_CHOICES = (
        (STATUS_ON, u"在职"),
        (STATUS_OFF, u"离职")
    )
    status = models.IntegerField(choices=STATUS_CHOICES, default=STATUS_ON, verbose_name=u'人员状态', null=True,
                                 blank=True, db_index=True)
    card_id = models.CharField(max_length=18, verbose_name=u'身份证号', null=True, blank=True, db_index=True)
    t1_card_no = models.CharField(max_length=20, verbose_name=u'T1证号', null=True, blank=True, db_index=True)
    # t1_authority = models.CharField(max_length=30, verbose_name=u'T1证发证机关', null=True, blank=True)
    t1_card_file = models.CharField(max_length=256, verbose_name=u"T1证附件网络地址", null=True, blank=True)
    t1_end_time = models.CharField(max_length=30, verbose_name=u'T1证有效期', null=True, blank=True)
    t2_card_no = models.CharField(max_length=20, verbose_name=u'T2证号', null=True, blank=True)
    # t2_authority = models.CharField(max_length=256, verbose_name=u'T2证发证机关', null=True, blank=True)
    t2_card_file = models.CharField(max_length=256, verbose_name=u"T2证附件网络地址", null=True, blank=True)
    t2_end_time = models.CharField(max_length=30, verbose_name=u'T2证有效期', null=True, blank=True)
    contract_start_time = models.CharField(max_length=30, verbose_name=u"劳动合同开始日期", null=True, blank=True)
    contract_end_time = models.CharField(max_length=30, verbose_name=u"劳动合同结束日期", null=True, blank=True)
    contract_file = models.CharField(max_length=4096, verbose_name=u"劳动合同附件网络地址", null=True, blank=True)
    social_security = models.CharField(max_length=1280, verbose_name=u"社保证明网络地址", null=True, blank=True)
    home_addr = models.CharField(max_length=256, verbose_name=u"家庭住址", null=True, blank=True)
    REGISTRY_1 = 10
    REGISTRY_2 = 20
    REGISTRY_3 = 30
    REGISTRY = (
        (10, u'未备案'),
        (20, u'备案成功'),
        (30, u'备案失败'),
    )
    registry_status = models.IntegerField(choices=REGISTRY, default=REGISTRY_1, verbose_name=u'备案状态', db_index=True)
    registry_time = models.DateTimeField(verbose_name=u'备案完成时间', null=True, blank=True)

    # class Meta:
    #     verbose_name = "维保人员信息表"
    #     verbose_name_plural = verbose_name
    #
    # def __unicode__(self):
    #     return self.auth_user.real_name


class Registry(models.Model):
    type_for_reg = models.CharField(max_length=30, verbose_name=u'备案类型', db_index=True)  # 如"CompanyInfo"
    reg_obj_id = models.IntegerField(verbose_name=u'备案对象在其model中的id', db_index=True)
    start_time = models.DateTimeField(verbose_name=u'备案开始时间', null=True, blank=True, db_index=True)
    end_time = models.DateTimeField(verbose_name=u'备案完成时间', null=True, blank=True, db_index=True)
    status = models.BooleanField(verbose_name=u'备案状态', default=False, db_index=True)
    msg = models.CharField(max_length=256, verbose_name=u'详情', default=u'')
    data = models.CharField(max_length=2560, verbose_name=u'返回数据', null=True, blank=True)
    reg_info = models.TextField(null=True, blank=True, verbose_name=u"备案提交的信息")
    ret_info = models.TextField(null=True, blank=True, verbose_name=u"备案时返回的信息")
    T_0 = 10
    T_1 = 20
    T_2 = 30
    T_3 = 40
    T_4 = 50
    T_S = (
        (10, u'非工单备案'),
        (20, u'首次维保备案'),
        (30, u'维保任务匹配备案'),
        (40, u'维保开始备案'),
        (50, u'维保完成备案'),
        (60, u'物业确认信息备案'),
    )
    task_type = models.IntegerField(choices=T_S, default=T_0, verbose_name=u"用来确认维保任务备案的阶段", db_index=True)


class Records(models.Model):
    user_agent = models.CharField(max_length=256, verbose_name=u'HTTP请求头user_agent', null=True, blank=True)
    path = models.CharField(max_length=1024, verbose_name=u'请求路径', null=True, blank=True)
    ip = models.CharField(max_length=20, verbose_name=u'remote ip', null=True, blank=True, db_index=True)
    request_time = models.DateTimeField(auto_now_add=True, verbose_name=u'请求时间', db_index=True)
    day_time = models.DateField(auto_now_add=True, verbose_name=u'请求时间，只有天', null=True, blank=True, db_index=True)
    user_id = models.IntegerField(verbose_name=u'用户id', null=True, blank=True, db_index=True)
    is_anonymous = models.BooleanField(verbose_name=u'是否匿名用户', default=False, db_index=True)
    parameters = models.CharField(max_length=4096, verbose_name=u'请求的参数', null=True, blank=True)
    method = models.CharField(max_length=10, verbose_name=u'请求的方法', null=True, blank=True, db_index=True)


class LogRefresh(BaseModel):
    refresh = models.BooleanField(u"是否实时刷新", default=False)


class SystemLog(BaseModel):
    create_by = models.CharField(max_length=20, verbose_name=u"操作者名称", default="")
    user = models.ForeignKey(AuthUser, verbose_name=u"操作者账户", null=True, blank=True, db_index=True)
    log = models.TextField(verbose_name=u"日志内容", default="")
    LT0 = 0
    LT1 = 1
    LT2 = 2
    LT = (
        (LT0, u"操作日志"),
        (LT1, u"系统日志"),
        (LT2, u"调试日志"),
    )
    log_type = models.SmallIntegerField(choices=LT, default=LT0, verbose_name=u"日志类型", db_index=True)


class PersonPositions(BaseModel):
    person = models.ForeignKey(Person, verbose_name=u'维保人员', null=True, db_index=True)
    position = models.CharField(max_length=125, verbose_name=u'维保人员经纬度', null=True, blank=True)
    is_new = models.BooleanField(default=True, verbose_name=u"是否是最新的位置", db_index=True)


class BusinessPermission(object):
    u"""业务权限"""

    # PERMISSION_ALL = "0"
    PERMISSION_NONE = "1"

    #  ---------------------------------维保--------------------------------------#

    # 工单
    PERMISSION_TASK_GET = "10"
    PERMISSION_TASK_PUT = "11"
    PERMISSION_TASK_DELETE = "12"
    PERMISSION_TASK_POST = "13"

    # 合同
    PERMISSION_CONTRACT_GET = "20"
    PERMISSION_CONTRACT_PUT = "21"
    PERMISSION_CONTRACT_DELETE = "22"
    PERMISSION_CONTRACT_POST = "23"

    # 电梯管理
    PERMISSION_ELEVATOR_GET = "30"
    PERMISSION_ELEVATOR_PUT = "31"
    PERMISSION_ELEVATOR_DELETE = "32"
    PERMISSION_ELEVATOR_POST = "33"

    # 电梯绑定
    PERMISSION_ELEVATOR_BIND_GET = "40"
    PERMISSION_ELEVATOR_BIND_PUT = "41"
    PERMISSION_ELEVATOR_BIND_DELETE = "42"
    PERMISSION_ELEVATOR_BIND_POST = "43"

    # 维保历史
    PERMISSION_MAINTAIN_HISTORY_GET = "50"
    PERMISSION_MAINTAIN_HISTORY_PUT = "51"
    PERMISSION_MAINTAIN_HISTORY_DELETE = "52"
    PERMISSION_MAINTAIN_HISTORY_POST = "53"

    # 维保单位
    PERMISSION_MAINTAIN_COMPANY_GET = "60"
    PERMISSION_MAINTAIN_COMPANY_PUT = "61"
    PERMISSION_MAINTAIN_COMPANY_DELETE = "62"
    PERMISSION_MAINTAIN_COMPANY_POST = "63"

    # 物业单位
    PERMISSION_PROPERTY_COMPANY_GET = "70"
    PERMISSION_PROPERTY_COMPANY_PUT = "71"
    PERMISSION_PROPERTY_COMPANY_DELETE = "72"
    PERMISSION_PROPERTY_COMPANY_POST = "73"

    # 维保人员
    PERMISSION_MAINTAIN_PERSON_GET = "80"
    PERMISSION_MAINTAIN_PERSON_PUT = "81"
    PERMISSION_MAINTAIN_PERSON_DELETE = "82"
    PERMISSION_MAINTAIN_PERSON_POST = "83"

    #  ---------------------------------维保--------------------------------------#
    #  ---------------------------------业务配置管理--------------------------------------#

    # 维修类型
    PERMISSION_REPAIR_TYPE_GET = "90"
    PERMISSION_REPAIR_TYPE_PUT = "91"
    PERMISSION_REPAIR_TYPE_DELETE = "92"
    PERMISSION_REPAIR_TYPE_POST = "93"

    # 维修项目
    PERMISSION_REPAIR_CONTENT_GET = "100"
    PERMISSION_REPAIR_CONTENT_PUT = "101"
    PERMISSION_REPAIR_CONTENT_DELETE = "102"
    PERMISSION_REPAIR_CONTENT_POST = "103"

    # 外设
    PERMISSION_PERIPHERAL_GET = "110"
    PERMISSION_PERIPHERAL_PUT = "111"
    PERMISSION_PERIPHERAL_DELETE = "112"
    PERMISSION_PERIPHERAL_POST = "113"

    #  ---------------------------------业务配置管理--------------------------------------#
    #  ---------------------------------账户--------------------------------------#

    # 维保单位账户
    PERMISSION_MAINTAIN_COMPANY_ACCOUNT_GET = "120"
    PERMISSION_MAINTAIN_COMPANY_ACCOUNT_PUT = "121"
    PERMISSION_MAINTAIN_COMPANY_ACCOUNT_DELETE = "122"
    PERMISSION_MAINTAIN_COMPANY_ACCOUNT_POST = "123"

    # 使用单位账户
    PERMISSION_PROPERTY_COMPANY_ACCOUNT_GET = "130"
    PERMISSION_PROPERTY_COMPANY_ACCOUNT_PUT = "131"
    PERMISSION_PROPERTY_COMPANY_ACCOUNT_DELETE = "132"
    PERMISSION_PROPERTY_COMPANY_ACCOUNT_POST = "133"

    # 日志
    PERMISSION_LOG_GET = "140"
    PERMISSION_LOG_PUT = "141"
    PERMISSION_LOG_DELETE = "142"
    PERMISSION_LOG_POST = "143"

    #  ---------------------------------账户--------------------------------------#
    #  ---------------------------------监控--------------------------------------#

    # 单梯监控
    PERMISSION_SINGLE_MONITORS_GET = "150"
    PERMISSION_SINGLE_MONITORS_PUT = "151"
    PERMISSION_SINGLE_MONITORS_DELETE = "152"
    PERMISSION_SINGLE_MONITORS_POST = "153"

    # 多梯监控
    PERMISSION_MULTIPLE_MONITORS_GET = "160"
    PERMISSION_MULTIPLE_MONITORS_PUT = "161"
    PERMISSION_MULTIPLE_MONITORS_DELETE = "163"
    PERMISSION_MULTIPLE_MONITORS_POST = "164"
    #  ---------------------------------监控--------------------------------------#
    #  ---------------------------------故障--------------------------------------#

    # 电梯故障记录
    PERMISSION_FAULT_RECORD_GET = "170"
    # 电梯故障数
    PERMISSION_FAULT_NUMBERS_STATISTICS_GET = "171"
    # 电梯使用地点故障数
    PERMISSION_FAULT_POSITION_STATISTICS_GET = "172"

    #  ---------------------------------故障--------------------------------------#
    #  ---------------------------------统计-------------------------------------yy-#

    # 电梯统计
    PERMISSION_ELEVATOR_STATISTICS = "180"
    # 电梯分类统计
    PERMISSION_ELEVATOR_CLASSIFICATION_STATISTICS = "181"
    # 电梯概况统计
    PERMISSION_ELEVATOR_SITUATION_STATISTICS = "182"
    # 电梯使用单位统计
    PERMISSION_ELEVATOR_PROPERTY_COMPANY_STATISTICS = "183"
    # 电梯维保单位统计
    PERMISSION_ELEVATOR_MAINTAIN_COMPANY_STATISTICS = "184"
    # 电梯品牌统计
    PERMISSION_ELEVATOR_BRAND_STATISTICS = "185"
    # 电梯维保状态统计
    PERMISSION_ELEVATOR_MAINTAIN_STATUS_STATISTICS = "186"
    # 电梯运行状态统计
    PERMISSION_ELEVATOR_RUN_STATUS_STATISTICS = "187"
    # 保
    # 电梯维修状态统计
    PERMISSION_ELEVATOR_REPAIR_STATUS_STATISTICS = "188"
    # 电梯救援状态统计
    PERMISSION_ELEVATOR_RESCUE_STATUS_STATISTICS = "189"

    #  ---------------------------------统计--------------------------------------#
    #  ---------------------------------角色--------------------------------------#

    # 角色 管理
    PERMISSION_ADMIN_ROLE_GET = "190"
    PERMISSION_ADMIN_ROLE_PUT = "191"
    PERMISSION_ADMIN_ROLE_DELETE = "192"
    PERMISSION_ADMIN_ROLE_POST = "193"

    PERMISSION_USER_GET = "200"
    PERMISSION_USER_PUT = "201"
    PERMISSION_USER_DELETE = "202"
    PERMISSION_USER_POST = "203"

    # 角色 关联权限
    # PERMISSION_ROLE_GET = "200"
    # PERMISSION_ROLE_PUT = "201"
    # PERMISSION_ROLE_DELETE = "202"
    # PERMISSION_ROLE_POST = "203"

    # 人授予角色
    # PERMISSION_ROLE_ADMIN_USER_GET = "210"
    # PERMISSION_ROLE_ADMIN_USER_PUT = "211"
    # PERMISSION_ROLE_ADMIN_USER_DELETE = "212"
    # PERMISSION_ROLE_ADMIN_USER_POST = "213"

    #  ---------------------------------角色--------------------------------------#

    #  --------------------------------- 维修管理 --------------------------------------#

    # 维修类型
    PERMISSION_REPAIR_GET = "220"
    PERMISSION_REPAIR_PUT = "221"
    PERMISSION_REPAIR_DELETE = "222"
    PERMISSION_REPAIR_POST = "223"

    #  ---------------------------------维修管理--------------------------------------#

    #  ---------------------------------首页统计--------------------------------------#
    PERMISSION_MONTH_GET = "230"
    PERMISSION_THIRD_GET = "231"
    PERMISSION_COUNT_GET = "232"
    PERMISSION_INDEX_GET = "233"

    #  ---------------------------------首页统计--------------------------------------#

    #  ---------------------------------组织管理--------------------------------------#
    PERMISSION_ORGANIZATION_GET = "240"
    PERMISSION_ORGANIZATION_PUT = "241"
    PERMISSION_ORGANIZATION_DELETE = "242"
    PERMISSION_ORGANIZATION_POST = "243"
    #  ---------------------------------组织管理--------------------------------------#

    # 制造单位
    PERMISSION_MANUFACTURER_GET = "250"
    PERMISSION_MANUFACTURER_PUT = "251"
    PERMISSION_MANUFACTURER_DELETE = "252"
    PERMISSION_MANUFACTURER_POST = "253"

    # 接入平台
    PERMISSION_PLATFORM_GET = "260"
    PERMISSION_PLATFORM_PUT = "261"
    PERMISSION_PLATFORM_DELETE = "262"
    PERMISSION_PLATFORM_POST = "263"

    # 平台概述
    PERMISSION_SUMMARY_GET = "270"

    # 故障管理
    PERMISSION_FAULT_STATISTICS_ANALYSIS = "280"

    # 电梯体检分析
    PERMISSION_CHECKUP_STATISTICS = "290"

    # 配件管理
    PERMISSION_FITTINGS_GET = "300"
    PERMISSION_FITTINGS_PUT = "301"
    PERMISSION_FITTINGS_DELETE = "302"
    PERMISSION_FITTINGS_POST = "303"

    # 故障会诊及分析
    PERMISSION_ELEVATOR_FAULT_ANALYSIS_GET = "310"
    PERMISSION_ELEVATOR_FAULT_ANALYSIS_PUT = "311"
    PERMISSION_ELEVATOR_FAULT_ANALYSIS_DELETE = "312"
    PERMISSION_ELEVATOR_FAULT_ANALYSIS_POST = "313"

    PERMISSION_MAP = [

        {
            "pids": [
                # {"pid": PERMISSION_MONTH_GET, "name": u"查看月电梯总数"},
                # {"pid": PERMISSION_THIRD_GET, "name": u"查看第三方电梯"},
                # {"pid": PERMISSION_COUNT_GET, "name": u"备案信息统计"}
                {"pid": PERMISSION_SUMMARY_GET, "name": u"平台概况", "index": "1-1", "desc": "get"}
            ],

            "name": u"平台概述",
            "depth": 1,
            "index": "1"
        },

        {
            "pids": [
                {"pid": PERMISSION_SINGLE_MONITORS_GET, "name": u"单梯监控查看", "index": "2-1", "desc": "get"},
                {"pid": PERMISSION_MULTIPLE_MONITORS_GET, "name": u"多梯监控查看", "index": "2-2", "desc": "get"}
            ],

            "name": u"电梯实时监控",
            "depth": 1,
            "index": "2"
        },

        {
             "pids": [
                 {"pid": PERMISSION_FAULT_RECORD_GET, "name": u"电梯故障记录", "index": "3-1", "desc": "get"},
                 # {"pid": PERMISSION_FAULT_STATISTICS_ANALYSIS, "name": u"电梯故障分析", "index": "3-2", "desc": "get"},
                 # {"pid": PERMISSION_FAULT_POSITION_STATISTICS_GET, "name": u"电梯使用地点故障数", "index": "3-3", "desc": "get"}
             ],
             "name": u"电梯故障管理",
             "depth": 1,
             "index": "3"
        },

        {
            "pids": [
                {"pid": PERMISSION_FAULT_STATISTICS_ANALYSIS, "name": u"电梯故障分析", "index": "4-1", "desc": "get"},
                {"pid": PERMISSION_CHECKUP_STATISTICS, "name": u"电梯体检分析", "index": "4-2", "desc": "get"},
                # {"pid": PERMISSION_ELEVATOR_STATISTICS, "name": u"电梯统计", "index": "4-1", "desc": "get"},
                # {"pid": PERMISSION_ELEVATOR_CLASSIFICATION_STATISTICS, "name": u"电梯分类统计", "index": "4-2", "desc": "get"},
                # {"pid": PERMISSION_ELEVATOR_SITUATION_STATISTICS, "name": u"电梯概况统计", "index": "4-3", "desc": "get"},
                # {"pid": PERMISSION_ELEVATOR_PROPERTY_COMPANY_STATISTICS, "name": u"电梯使用单位统计", "index": "4-4", "desc": "get"},
                # {"pid": PERMISSION_ELEVATOR_MAINTAIN_COMPANY_STATISTICS, "name": u"电梯维保单位统计", "index": "4-5", "desc": "get"},
                # {"pid": PERMISSION_ELEVATOR_BRAND_STATISTICS, "name": u"电梯品牌统计", "index": "4-6", "desc": "get"},
                # {"pid": PERMISSION_ELEVATOR_MAINTAIN_STATUS_STATISTICS, "name": u"电梯维保状态统计", "index": "4-7", "desc": "get"},
                # {"pid": PERMISSION_ELEVATOR_RUN_STATUS_STATISTICS, "name": u"电梯运行状态统计", "index": "4-8", "desc": "get"},
                # {"pid": PERMISSION_ELEVATOR_REPAIR_STATUS_STATISTICS, "name": u"电梯维修状态统计", "index": "4-9", "desc": "get"},
                # {"pid": PERMISSION_ELEVATOR_RESCUE_STATUS_STATISTICS, "name": u"电梯救援状态统计", "index": "4-10", "desc": "get"},
            ],
            "name": u"电梯数据统计",
            "depth": 1,
            "index": "4"
        },

        {
        #     PERMISSION_COUNT_GET
        # {"pid": PERMISSION_SUMMARY_GET, "name": u"平台概述", "index": "1-1", "desc": "get"}
        #

            "pids": [
                {
                        "pids": [
                        {"pid": PERMISSION_INDEX_GET, "name": u"维保概况", "index": "5-1", "desc": "get"}
                        ],
                        "name": u"维保概况",
                        "depth": 1,
                        "index": "5-1"
                },

                {
                    "pids": [
                        {"pid": PERMISSION_TASK_GET, "name": u"工单查看", "index": "5-2-1", "desc": "get"},
                        {"pid": PERMISSION_TASK_PUT, "name": u"工单更新", "index": "5-2-2", "desc": "put"},
                        {"pid": PERMISSION_TASK_DELETE, "name": u"工单删除", "index": "5-2-3", "desc": "delete"},
                        {"pid": PERMISSION_TASK_POST, "name": u"工单创建", "index": "5-2-4", "desc": "post"},
                    ],
                    "name": u"保养计划",
                    "depth": 1,
                    "index": "5-2"
                },

                {
                    "pids": [
                        {"pid": PERMISSION_CONTRACT_GET, "name": u"合同查看", "index": "5-3-1", "desc": "get"},
                        {"pid": PERMISSION_CONTRACT_PUT, "name": u"合同更新", "index": "5-3-2", "desc": "put"},
                        {"pid": PERMISSION_CONTRACT_DELETE, "name": u"合同删除", "index": "5-3-3", "desc": "delete"},
                        {"pid": PERMISSION_CONTRACT_POST, "name": u"合同创建", "index": "5-3-4", "desc": "post"},
                    ],
                    "name": u"维保合同",
                    "depth": 1,
                    "index": "5-3"
                },



                {
                    "pids": [
                        {"pid": PERMISSION_ELEVATOR_BIND_GET, "name": u"电梯绑定查看", "index": "5-4-1", "desc": "get"},
                        {"pid": PERMISSION_ELEVATOR_BIND_PUT, "name": u"电梯绑定更新", "index": "5-4-2", "desc": "put"},
                        {"pid": PERMISSION_ELEVATOR_BIND_DELETE, "name": u"电梯绑定删除", "index": "5-4-3", "desc": "delete"},
                        {"pid": PERMISSION_ELEVATOR_BIND_POST, "name": u"电梯绑定创建", "index": "5-4-4", "desc": "post"},
                    ],
                    "name": u"电梯绑定",
                    "depth": 1,
                    "index": "5-4"
                },

                {
                    "pids": [
                        {"pid": PERMISSION_MAINTAIN_HISTORY_GET, "name": u"维保历史查看", "index": "5-5-1", "desc": "get"},
                        {"pid": PERMISSION_MAINTAIN_HISTORY_PUT, "name": u"维保历史更新", "index": "5-5-2", "desc": "put"},
                        {"pid": PERMISSION_MAINTAIN_HISTORY_DELETE, "name": u"维保历史删除", "index": "5-5-3", "desc": "delete"},
                        {"pid": PERMISSION_MAINTAIN_HISTORY_POST, "name": u"维保历史创建", "index": "5-5-4", "desc": "post"},
                    ],
                    "name": u"维保历史",
                    "depth": 1,
                    "index": "5-5"
                },



                # {
                #     "pids": [
                #         {"pid": PERMISSION_MAINTAIN_PERSON_GET, "name": u"维保人员查看"},
                #         {"pid": PERMISSION_MAINTAIN_PERSON_PUT, "name": u"维保人员更新"},
                #         {"pid": PERMISSION_MAINTAIN_PERSON_DELETE, "name": u"维保人员删除"},
                #         {"pid": PERMISSION_MAINTAIN_PERSON_POST, "name": u"维保人员创建"},
                #     ],
                #     "name": u"维保人员",
                #     "depth": 1
                # },
            ],
            "name": u"电梯保养",
            "depth": 2,
            "index": "5"
        },

        {
            "pids": [
                # {
                #     "pids": [
                #
                #     ],
                #     "name": u"物联网报警",
                #     "depth": 1
                # },



                {
                    "pids": [
                        {"pid": PERMISSION_REPAIR_GET, "name": u"维修查看", "index": "6-2-1", "desc": "get"},
                        {"pid": PERMISSION_REPAIR_PUT, "name": u"维修更新", "index": "6-2-2", "desc": "put"},
                        {"pid": PERMISSION_REPAIR_DELETE, "name": u"维修删除", "index": "6-2-3", "desc": "delete"},
                        {"pid": PERMISSION_REPAIR_POST, "name": u"维修创建", "index": "6-2-4", "desc": "post"},
                    ],
                    "name": u"普通维修",
                    "depth": 1,
                    "index": "6-2"
                },
                {
                    "pids": [
                        {"pid": PERMISSION_FITTINGS_GET, "name": u"配件查看", "index": "6-3-1", "desc": "get"},
                        {"pid": PERMISSION_FITTINGS_PUT, "name": u"配件更新", "index": "6-3-2", "desc": "put"},
                        {"pid": PERMISSION_FITTINGS_DELETE, "name": u"配件删除", "index": "6-3-3", "desc": "delete"},
                        {"pid": PERMISSION_FITTINGS_POST, "name": u"配件创建", "index": "6-3-4", "desc": "post"},
                    ],
                    "name": u"配件管理",
                    "depth": 1,
                    "index": "6-3"
                },
            ],
            "name": u"维修管理",
            "depth": 2,
            "index": "6"
        },

        {
            "pids": [
                {
                    "pids": [
                        {"pid": PERMISSION_ELEVATOR_FAULT_ANALYSIS_GET, "name": u"故障知识库查看", "index": "9-1-1", "desc": "get"},
                        {"pid": PERMISSION_ELEVATOR_FAULT_ANALYSIS_PUT, "name": u"故障知识库更新", "index": "9-1-2", "desc": "put"},
                        {"pid": PERMISSION_ELEVATOR_FAULT_ANALYSIS_DELETE, "name": u"故障知识库删除", "index": "9-1-3", "desc": "delete"},
                        {"pid": PERMISSION_ELEVATOR_FAULT_ANALYSIS_POST, "name": u"故障知识库创建", "index": "9-1-4", "desc": "post"},
                    ],
                    "name": u"故障知识库",
                    "depth": 1,
                    "index": "9-1"
                },

            ],
            "name": u"故障会诊及分析",
            "depth": 2,
            "index": "9"
        },


        {
            "pids": [

                {
                    "pids": [
                        {"pid": PERMISSION_ELEVATOR_GET, "name": u"电梯查看", "index": "10-1-1", "desc": "get"},
                        {"pid": PERMISSION_ELEVATOR_PUT, "name": u"电梯更新", "index": "10-1-2", "desc": "put"},
                        {"pid": PERMISSION_ELEVATOR_DELETE, "name": u"电梯删除", "index": "10-1-3", "desc": "delete"},
                        {"pid": PERMISSION_ELEVATOR_POST, "name": u"电梯创建", "index": "10-1-4", "desc": "post"},
                    ],
                    "name": u"电梯管理",
                    "depth": 1,
                    "index": "10-1"
                },

                # {
                #     "pids": [
                #         {"pid": PERMISSION_REPAIR_TYPE_GET, "name": u"维修类型查看", "index": "7-2-1", "desc": "get"},
                #         {"pid": PERMISSION_REPAIR_TYPE_PUT, "name": u"维修类型更新", "index": "7-2-2", "desc": "put"},
                #         {"pid": PERMISSION_REPAIR_TYPE_DELETE, "name": u"维修类型删除", "index": "7-2-3", "desc": "delete"},
                #         {"pid": PERMISSION_REPAIR_TYPE_POST, "name": u"维修类型创建", "index": "7-2-4", "desc": "post"},
                #     ],
                #     "name": u"维修类型",
                #     "depth": 1,
                #     "index": "7-2"
                # },

                {
                    "pids": [
                        {"pid": PERMISSION_REPAIR_CONTENT_GET, "name": u"维修项目查看", "index": "10-2-1", "desc": "get"},
                        {"pid": PERMISSION_REPAIR_CONTENT_PUT, "name": u"维修项目更新", "index": "10-2-2", "desc": "put"},
                        {"pid": PERMISSION_REPAIR_CONTENT_DELETE, "name": u"维修项目删除", "index": "10-2-3", "desc": "delete"},
                        {"pid": PERMISSION_REPAIR_CONTENT_POST, "name": u"维修项目创建", "index": "10-2-4", "desc": "post"},
                    ],
                    "name": u"维修项目",
                    "depth": 1,
                    "index": "10-2"
                },

                {
                    "pids": [
                        {"pid": PERMISSION_PERIPHERAL_GET, "name": u"外设查看", "index": "10-3-1", "desc": "get"},
                        {"pid": PERMISSION_PERIPHERAL_PUT, "name": u"外设更新", "index": "10-3-2", "desc": "put"},
                        {"pid": PERMISSION_PERIPHERAL_DELETE, "name": u"外设删除", "index": "10-3-3", "desc": "delete"},
                        {"pid": PERMISSION_PERIPHERAL_POST, "name": u"外设创建", "index": "10-3-4", "desc": "post"},
                    ],
                    "name": u"外设管理",
                    "depth": 1,
                    "index": "10-3"
                },

                {
                    "pids": [
                        {"pid": PERMISSION_MANUFACTURER_GET, "name": u"制造单位查看", "index": "10-4-1", "desc": "get"},
                        {"pid": PERMISSION_MANUFACTURER_PUT, "name": u"制造单位更新", "index": "10-4-2", "desc": "put"},
                        {"pid": PERMISSION_MANUFACTURER_DELETE, "name": u"制造单位删除", "index": "10-4-3", "desc": "delete"},
                        {"pid": PERMISSION_MANUFACTURER_POST, "name": u"制造单位创建", "index": "10-4-4", "desc": "post"},
                    ],
                    "name": u"制造单位管理",
                    "depth": 1,
                    "index": "10-4"
                },

                {
                    "pids": [
                        {"pid": PERMISSION_PLATFORM_GET, "name": u"平台查看", "index": "10-5-1", "desc": "get"},
                        {"pid": PERMISSION_PLATFORM_PUT, "name": u"平台更新", "index": "10-5-2", "desc": "put"},
                        {"pid": PERMISSION_PLATFORM_DELETE, "name": u"平台删除", "index": "10-5-3", "desc": "delete"},
                        {"pid": PERMISSION_PLATFORM_POST, "name": u"平台设建", "index": "10-5-4", "desc": "post"},
                    ],
                    "name": u"厂家接入控制",
                    "depth": 1,
                    "index": "10-5"
                },


            ],
            "name": u"业务配置管理",
            "depth": 2,
            "index": "10"
        },

        {
            "pids": [
                # {
                #     "pids": [
                #         {"pid": PERMISSION_MAINTAIN_COMPANY_ACCOUNT_GET, "name": u"维保单位账户查看"},
                #         {"pid": PERMISSION_MAINTAIN_COMPANY_ACCOUNT_PUT, "name": u"维保单位账户更新"},
                #         {"pid": PERMISSION_MAINTAIN_COMPANY_ACCOUNT_DELETE, "name": u"维保单位账户删除"},
                #         {"pid": PERMISSION_MAINTAIN_COMPANY_ACCOUNT_POST, "name": u"维保单位账户创建"},
                #     ],
                #     "name": u"维保单位",
                #     "depth": 1
                # },
                #
                # {
                #     "pids": [
                #         {"pid": PERMISSION_PROPERTY_COMPANY_ACCOUNT_GET, "name": u"物业单位账户查看"},
                #         {"pid": PERMISSION_PROPERTY_COMPANY_ACCOUNT_PUT, "name": u"物业单位账户更新"},
                #         {"pid": PERMISSION_PROPERTY_COMPANY_ACCOUNT_DELETE, "name": u"物业单位账户删除"},
                #         {"pid": PERMISSION_PROPERTY_COMPANY_ACCOUNT_POST, "name": u"物业单位账户创建"},
                #     ],
                #     "name": u"物业单位",
                #     "depth": 1
                # },

                {
                    "pids": [
                        {"pid": PERMISSION_ADMIN_ROLE_GET, "name": u"角色查看", "index": "11-1-1", "desc": "get"},
                        {"pid": PERMISSION_ADMIN_ROLE_PUT, "name": u"角色更新", "index": "11-1-2", "desc": "put"},
                        {"pid": PERMISSION_ADMIN_ROLE_DELETE, "name": u"角色删除", "index": "11-1-3", "desc": "delete"},
                        {"pid": PERMISSION_ADMIN_ROLE_POST, "name": u"角色添加", "index": "11-1-4", "desc": "post"},
                    ],
                    "name": u"角色管理",
                    "depth": 1,
                    "index": "11-1"
                },

                # {
                #     "pids": [
                #         {"pid": PERMISSION_ROLE_GET, "name": u"角色关联权限查看"},
                #         {"pid": PERMISSION_ROLE_PUT, "name": u"角色关联权限更新"},
                #         {"pid": PERMISSION_ROLE_DELETE, "name": u"角色关联权限删除"},
                #         {"pid": PERMISSION_ROLE_POST, "name": u"角色关联权限添加"},
                #     ],
                #     "name": u"",
                #     "depth": 1
                # },

                # {
                #     "pids": [
                #         {"pid": PERMISSION_MAINTAIN_COMPANY_GET, "name": u"维保单位查看", "index": "8-2-1", "desc": "get"},
                #         {"pid": PERMISSION_MAINTAIN_COMPANY_PUT, "name": u"维保单位更新", "index": "8-2-2", "desc": "put"},
                #         {"pid": PERMISSION_MAINTAIN_COMPANY_DELETE, "name": u"维保单位删除", "index": "8-2-3", "desc": "delete"},
                #         {"pid": PERMISSION_MAINTAIN_COMPANY_POST, "name": u"维保单位创建", "index": "8-2-4", "desc": "post"},
                #     ],
                #     "name": u"维保单位管理",
                #     "depth": 1,
                #     "index": "8-2"
                # },

                # {
                #     "pids": [
                #         {"pid": PERMISSION_PROPERTY_COMPANY_GET, "name": u"使用单位查看", "index": "8-3-1", "desc": "get"},
                #         {"pid": PERMISSION_PROPERTY_COMPANY_PUT, "name": u"使用单位更新", "index": "8-3-2", "desc": "put"},
                #         {"pid": PERMISSION_PROPERTY_COMPANY_DELETE, "name": u"使用单位删除", "index": "8-3-3", "desc": "delete"},
                #         {"pid": PERMISSION_PROPERTY_COMPANY_POST, "name": u"使用单位创建", "index": "8-3-4", "desc": "post"},
                #     ],
                #     "name": u"使用单位管理",
                #     "depth": 1,
                #     "index": "8-3"
                # },

                {
                    "pids": [
                        {"pid": PERMISSION_USER_GET, "name": u"用户查看", "index": "11-2-1", "desc": "get"},
                        {"pid": PERMISSION_USER_PUT, "name": u"用户更新", "index": "11-2-2", "desc": "put"},
                        {"pid": PERMISSION_USER_DELETE, "name": u"用户删除", "index": "11-2-3", "desc": "delete"},
                        {"pid": PERMISSION_USER_POST, "name": u"用户创建", "index": "11-2-4", "desc": "post"},
                    ],
                    "name": u"用户管理",
                    "depth": 1,
                    "index": "11-2"
                },

                {
                    "pids": [
                        {"pid": PERMISSION_MAINTAIN_COMPANY_GET, "name": u"单位查看", "index": "11-3-1", "desc": "get"},
                        {"pid": PERMISSION_MAINTAIN_COMPANY_PUT, "name": u"单位更新", "index": "11-3-2", "desc": "put"},
                        {"pid": PERMISSION_MAINTAIN_COMPANY_DELETE, "name": u"单位删除", "index": "11-3-3", "desc": "delete"},
                        {"pid": PERMISSION_MAINTAIN_COMPANY_POST, "name": u"单位创建", "index": "11-3-4", "desc": "post"},
                    ],
                    "name": u"单位管理",
                    "depth": 1,
                    "index": "11-3"
                },

                # {
                #     "pids": [
                #         {"pid": PERMISSION_ORGANIZATION_GET, "name": u"组织查看", "index": "8-5-1", "desc": "get"},
                #         {"pid": PERMISSION_ORGANIZATION_PUT, "name": u"组织更新", "index": "8-5-2", "desc": "put"},
                #         {"pid": PERMISSION_ORGANIZATION_DELETE, "name": u"组织删除", "index": "8-5-3", "desc": "delete"},
                #         {"pid": PERMISSION_ORGANIZATION_POST, "name": u"组织创建", "index": "8-5-4", "desc": "post"},
                #     ],
                #     "name": u"组织管理",
                #     "depth": 1,
                #     "index": "8-5"
                # },

                {
                    "pids": [
                        {"pid": PERMISSION_LOG_GET, "name": u"日志查看", "index": "11-4-1", "desc": "get"},
                        # {"pid": PERMISSION_LOG_PUT, "name": u"日志更新", "index": "8-4-2", "desc": "put"},
                        # {"pid": PERMISSION_LOG_DELETE, "name": u"日志删除", "index": "8-4-3", "desc": "delete"},
                        # {"pid": PERMISSION_LOG_POST, "name": u"日志创建", "index": "8-4-4", "desc": "post"},
                    ],
                    "name": u"日志",
                    "depth": 1,
                    "index": "11-4"
                },

            ],
            "name": u"系统管理",
            "depth": 2,
            "index": "11"
        },

        # {"pid": PERMISSION_NONE, "name": u"无权限", "pdesc": u"没有任何权限"},
        # {"pid": PERMISSION_ALL, "name": u"所有权限", "pdesc": u"超管专用"},
    ]


class AdminRole(BaseModel):
    u"""用户管理员角色"""
    role_name = models.CharField(verbose_name=u"角色名称", max_length=64, db_index=True)
    role_desc = models.CharField(verbose_name=u"角色描述", max_length=256)
    RT1 = 0
    RT2 = 10
    RT3 = 20
    RT4 = 30
    RT5 = 40
    RT = (
        (RT1, u"平台管理员"),
        (RT2, u"维保人员"),
        (RT3, u"维保单位"),
        (RT4, u"使用单位"),
        (RT5, u"监管单位"),
    )
    role_type = models.IntegerField(choices=RT, verbose_name=u"角色类型", default=RT2, db_index=True)
    company = models.ForeignKey(CompanyInfo, verbose_name=u"具体某个单位的角色", null=True)
    # management = models.ForeignKey('OrganizationManagement', verbose_name=u"所属部门组织", db_index=True, null=True)
    create_by = models.ForeignKey(AuthUser, verbose_name=u"创建人", db_index=True, null=True)
    MT1 = 10
    MT2 = 20
    MT = (
        (MT1, u"部门"),
        (MT2, u"自选"),
    )
    management_type = models.SmallIntegerField(default=MT1, choices=MT, verbose_name=u"数据权限类型", db_index=True)
    CFA0 = 0
    CFA1 = 1
    CFA = (
        (CFA0, u"否"),
        (CFA1, u"是")
    )
    create_for_all = models.SmallIntegerField(default=CFA0, choices=CFA, verbose_name=u"是否全系统可用", db_index=True)

    def permissions(self):
        rbp = RoleBusinessPermission.objects.filter_active(admin_role=self)
        if not rbp:
            return []
        ps = rbp.values_list("permission", flat=True).distinct()
        return ps

    def register_codes(self):
        return ElevatorAdminRoRelation.objects.filter_active(admin_role=self).values_list("register_code", flat=True).distinct()

    def management(self):
        oarr = OrganizationAdminRoRelation.objects.filter_active(admin_role=self).first()
        data = {"id": None, "name": None}
        if oarr:
            if oarr.organization:
                data = {"id": oarr.organization.id, "name": oarr.organization.name}
            elif oarr.company:
                data = {"id": oarr.company.id, "name": oarr.company.name}
            elif oarr.is_top:
                data = {"id": -1, "name": u"梯联网"}
        return data

    def create_name(self):
        return self.create_by.username if self.create_by else None


class RoleBusinessPermission(BaseModel):
    u"""角色权限"""
    admin_role = models.ForeignKey(AdminRole, db_index=True)
    permission = models.CharField(u"权限ID", db_index=True, default="1", max_length=4)


# class OrganizationManagement(BaseModel):
#     """ 废弃 """
#     name = models.CharField(max_length=100, null=True, db_index=True, verbose_name=u"组织名称")
#     T0 = 0
#     T1 = 10
#     T2 = 20
#     T3 = 30
#     T = (
#         (T0, u"平台管理"),
#         (T1, u"监管部门"),
#         (T2, u"使用单位部门"),
#         (T3, u"维保单位部门"),
#     )
#     org_type = models.SmallIntegerField(choices=T, default=T3, db_index=True, verbose_name=u"类型")


class Organization(BaseModel):
    u""" 组织结构 """
    company = models.ForeignKey(CompanyInfo, verbose_name=u"单位", null=True, blank=True, db_index=True)
    name = models.CharField(max_length=100, null=True, db_index=True, verbose_name=u"组织名称")
    parent = models.ForeignKey('self', null=True, verbose_name=u"上级部门")
    parent_name = models.CharField(max_length=100, null=True, db_index=True, verbose_name=u"父组织名称")
    serial_num = models.SmallIntegerField(verbose_name=u"同级序号", default=0)
    depth_id = models.SmallIntegerField(verbose_name=u"第几级组织", default=0)

    def children(self):
        u""" 某个组织的子孙 """
        ret = [self.id]
        sons = Organization.objects.filter_active(parent=self)
        for son in sons:
            son_ret = son.children()
            if son_ret:
                ret.extend(son_ret)
        return ret

    def json(self):
        return {
            "company": self.company.id,
            "name": self.name,
            "parent": self.parent.id,
            "parent_name": self.parent_name,
            "serial_num": self.serial_num,
            "depth_id": self.depth_id,
            "children": list()
        }

    def list(self):
        return list()


class OrganizationElevatorRelation(BaseModel):
    u""" 组织关联电梯 """
    organization = models.ForeignKey(Organization, null=True, db_index=True, verbose_name=u"组织")
    register_code = models.CharField(max_length=20, verbose_name=u"电梯注册代码", default=True)
    internal_no = models.CharField(max_length=50, verbose_name=u"内部编号", null=True, blank=True, db_index=True)
    maintain_company_name = models.CharField(max_length=256, verbose_name=u"维保单位名称", null=True, blank=True)
    property_company_name = models.CharField(max_length=256, verbose_name=u"使用单位名称", null=True, blank=True)
    is_business = models.BooleanField(default=True, verbose_name=u"是否是维保商业电梯")


class OrganizationAdminRoRelation(BaseModel):
    u""" 组织关联角色"""
    organization = models.ForeignKey(Organization, null=True, db_index=True, verbose_name=u"组织")
    admin_role = models.ForeignKey(AdminRole, db_index=True, null=True, verbose_name=u"角色")
    company = models.ForeignKey(CompanyInfo, verbose_name=u"单位", null=True, blank=True)
    is_top = models.BooleanField(default=False, verbose_name=u"是否是顶级组织")


class ElevatorAdminRoRelation(BaseModel):
    u""" 角色直接关联电梯 """
    admin_role = models.ForeignKey(AdminRole, db_index=True, null=True, verbose_name=u"角色")
    register_code = models.CharField(max_length=20, verbose_name=u"电梯注册代码", null=True, db_index=True)
    is_business = models.BooleanField(default=True, verbose_name=u"是否是维保商业电梯")
    internal_no = models.CharField(max_length=50, verbose_name=u"内部编号", null=True, blank=True, db_index=True)
    maintain_company_name = models.CharField(max_length=256, verbose_name=u"维保单位名称", null=True, blank=True)
    property_company_name = models.CharField(max_length=256, verbose_name=u"使用单位名称", null=True, blank=True)


class BasicSupervisedRelation(BaseModel):
    register_code = models.CharField(max_length=20, verbose_name=u"电梯注册代码", default=True)
    internal_no = models.CharField(max_length=50, verbose_name=u"内部编号", null=True, blank=True, db_index=True)
    maintain_company_name = models.CharField(max_length=256, verbose_name=u"维保单位名称", null=True, blank=True)
    property_company_name = models.CharField(max_length=256, verbose_name=u"使用单位名称", null=True, blank=True)



































































